﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
//提供了一个方法集，这些方法用于分配非托管内存、复制非托管内存块、将托管类型转换为非托管类型，此外还提供了在与非托管代码交互时使用的其他杂项方法
using System.Runtime.InteropServices;
using System.Net;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace DotNet.Utilities.Entity帮助类
{
    public static class InteropHelper
    {
        public const int ERROR_ALREADY_EXISTS = 0183;

        private static string[] strEncrypt = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP" };
        private static Int32 METHOD_BUFFERED = 0;
        private static Int32 FILE_ANY_ACCESS = 0;
        private static Int32 FILE_DEVICE_HAL = 0x00000101;
        private const Int32 ERROR_NOT_SUPPORTED = 0x32;
        private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;
        private static Int32 IOCTL_HAL_GET_DEVICEID = ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) | ((21) << 2) | (METHOD_BUFFERED);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool KernelIoControl(Int32 dwIoControlCode, IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, Int32 nOutBufSize, ref Int32 lpBytesReturned);
        [DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
        public static extern uint SendARP(uint DestIP, uint SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
        /// <summary> 
        /// 获取MAC地址 
        /// </summary> 
        /// <returns></returns> 
        public static bool GetMac(out string mac)
        {
            uint ip = 0;
            //string mac = string.Empty;
            //取本机IP列表 
            IPAddress[] ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList;

            //取本机IP 
            byte[] ipp = ips[0].GetAddressBytes();
            ip = (uint)((ipp[0]) | (ipp[1] << 8) | (ipp[2] << 16) | (ipp[3] << 24));
            //取MAC 
            byte[] MacAddr = new byte[6];
            uint PhyAddrLen = 6;
            uint hr = SendARP(ip, 0, MacAddr, ref PhyAddrLen);
            if (MacAddr[0] != 0 || MacAddr[1] != 0 || MacAddr[2] != 0 || MacAddr[3] != 0 || MacAddr[4] != 0 || MacAddr[5] != 0)
            {
                mac = MacAddr[0].ToString("X2") + ":" + MacAddr[1].ToString("X2") + ":" + MacAddr[2].ToString("X2") + ":" + MacAddr[3].ToString("X2") + ":" + MacAddr[4].ToString("X2") + ":" + MacAddr[5].ToString("X2");
                return true;
            }
            else
            {
                mac = "设备未联网，请联网后再启动！";
                return false;
            }
        }

        /// <summary>
        /// 获取16位MD5值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetStringMD5(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] buffer = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            return BitConverter.ToString(buffer, 4, 8).Replace("-", "");
        }

        [DllImport("coredll.Dll")]
        public static extern int ReleaseMutex(IntPtr hMutex);

        [DllImport("coredll.dll", EntryPoint = "CreateMutex", SetLastError = true)]
        public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);

        [DllImport("coredll.dll", EntryPoint = "FindWindowW", SetLastError = true)]
        public static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);

        [DllImport("coredll.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("coredll.dll")]
        public static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);

        [DllImport("coredll.dll")]
        public static extern bool CloseHandle(IntPtr handle);

        [DllImport("coredll.dll")]
        public static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);

        private const int PROCESS_TERMINATE = 1;
    }
}
